﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace GTA_V_Handling_Editor
{
    public partial class HandlingEditor : Form
    {
        public HandlingEditor()
        {
            InitializeComponent();
        }
        MemoryStream FileData;
        HandlingData Vehicles;
        bool scriptfocus = false;
        bool updating = true;
        string tempdata;
        string Filename = "handling.dat";
        Data updatecardata(Data data)
        {
            data.Handling[0] = txtB.Text;
            data.Handling[1] = txtC.Text;
            data.Handling[2] = txtD.Text;
            data.Handling[3] = txtE.Text;
            data.Handling[4] = txtF.Text;
            data.Handling[5] = txtG.Text;
            data.Handling[6] = txtCx.Text;
            data.Handling[7] = txtCy.Text;
            data.Handling[8] = txtCz.Text;
            data.Handling[9] = txtJx.Text;
            data.Handling[10] = txtJy.Text;
            data.Handling[11] = txtJz.Text;
            data.Handling[12] = txtTt.Text;
            data.Handling[13] = txtTg.Text;
            data.Handling[14] = txtTf.Text;
            data.Handling[15] = txtTi.Text;
            data.Handling[16] = txtTCu.Text;
            data.Handling[17] = txtTCd.Text;
            data.Handling[18] = txtTv.Text;
            data.Handling[19] = txtTb.Text;
            data.Handling[20] = txtTbb.Text;
            data.Handling[21] = txtThb.Text;
            data.Handling[22] = txtTs.Text;
            data.Handling[23] = txtWcPlus.Text;
            data.Handling[24] = txtWcMinus.Text;
            data.Handling[25] = txtWcLat.Text;
            data.Handling[26] = txtWsPlus.Text;
            data.Handling[27] = txtWlT.Text;
            data.Handling[28] = txtWCs.Text;
            data.Handling[29] = txtWh.Text;
            data.Handling[30] = txtWMtL.Text;
            data.Handling[31] = txtSf.Text;
            data.Handling[32] = txtScd.Text;
            data.Handling[33] = txtSrd.Text;
            data.Handling[34] = txtSu.Text;
            data.Handling[35] = txtSl.Text;
            data.Handling[36] = txtSr.Text;
            data.Handling[37] = txtSb.Text;
            data.Handling[38] = txtArbF.Text;
            data.Handling[39] = txtArbB.Text;
            data.Handling[40] = txtRCf.Text;
            data.Handling[41] = txtRCr.Text;
            data.Handling[42] = txtDc.Text;
            data.Handling[43] = txtDw.Text;
            data.Handling[44] = txtDd.Text;
            data.Handling[45] = txtDe.Text;
            data.Handling[46] = txtPet.Text;
            data.Handling[47] = txtOil.Text;
            data.Handling[48] = txtMsX.Text;
            data.Handling[49] = txtMsY.Text;
            data.Handling[50] = txtMsZ.Text;
            data.Handling[51] = txtMv.Text;
            data.Handling[52] = txtMmf.Text;
            data.Handling[53] = txtMhf.Text;
            data.Handling[54] = txtMa.Text;
            data.Handling[55] = txtMdf.Text;
            data.Handling[56] = txtDifficult.Text;
            return data;
        }
        
        void updatecartxtboxes(Data data)
        {
            updating = true;
            txtB.Text = data.Handling[0];
            txtC.Text = data.Handling[1];
            txtD.Text = data.Handling[2];
            txtE.Text = data.Handling[3];
            txtF.Text = data.Handling[4];
            txtG.Text = data.Handling[5];
            txtCx.Text = data.Handling[6];
            txtCy.Text = data.Handling[7];
            txtCz.Text = data.Handling[8];
            txtJx.Text = data.Handling[9];
            txtJy.Text = data.Handling[10];
            txtJz.Text = data.Handling[11];
            txtTt.Text = data.Handling[12];
            txtTg.Text = data.Handling[13];
            txtTf.Text = data.Handling[14];
            txtTi.Text = data.Handling[15];
            txtTCu.Text = data.Handling[16];
            txtTCd.Text = data.Handling[17];
            txtTv.Text = data.Handling[18];
            txtTb.Text = data.Handling[19];
            txtTbb.Text = data.Handling[20];
            txtThb.Text = data.Handling[21];
            txtTs.Text = data.Handling[22];
            txtWcPlus.Text = data.Handling[23];
            txtWcMinus.Text = data.Handling[24];
            txtWcLat.Text = data.Handling[25];
            txtWsPlus.Text = data.Handling[26];
            txtWlT.Text = data.Handling[27];
            txtWCs.Text = data.Handling[28];
            txtWh.Text = data.Handling[29];
            txtWMtL.Text = data.Handling[30];
            txtSf.Text = data.Handling[31];
            txtScd.Text = data.Handling[32];
            txtSrd.Text = data.Handling[33];
            txtSu.Text = data.Handling[34];
            txtSl.Text = data.Handling[35];
            txtSr.Text = data.Handling[36];
            txtSb.Text = data.Handling[37];
            txtArbF.Text = data.Handling[38];
            txtArbB.Text = data.Handling[39];
            txtRCf.Text = data.Handling[40];
            txtRCr.Text = data.Handling[41];
            txtDc.Text = data.Handling[42];
            txtDw.Text = data.Handling[43];
            txtDd.Text = data.Handling[44];
            txtDe.Text = data.Handling[45];
            txtPet.Text = data.Handling[46];
            txtOil.Text = data.Handling[47];
            txtMsX.Text = data.Handling[48];
            txtMsY.Text = data.Handling[49];
            txtMsZ.Text = data.Handling[50];
            txtMv.Text = data.Handling[51];
            txtMmf.Text = data.Handling[52];
            txtMhf.Text = data.Handling[53];
            txtMa.Text = data.Handling[54];
            txtMdf.Text = data.Handling[55];
            txtDifficult.Text = data.Handling[56];
            updating = false;
        }
        

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog { Filter = "Handling files|handling*.dat|data files|*.dat|All Files|*.*", FileName = new FileInfo(Filename).Name };
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Filename = ofd.FileName;
                FileData = new MemoryStream(File.ReadAllBytes(Filename));
                Vehicles = Utils.GetVehicles(FileData);
                fillboxes(CarDropDown, CarVehicleID, Vehicles.Cars);
                fillboxes(boatDropDown, boatVehicleID, Vehicles.Boats);
                fillboxes(bikeDropDown, bikeVehicleID, Vehicles.Bikes);
                fillboxes(planeDropDown, planeVehicleID, Vehicles.Flying);
                fillboxes(weapDropDown, weapVehicleID, Vehicles.Weapons);
                fillboxes(subDropDown, subVehicleID, Vehicles.Submarines);
                fillboxes(trainDropDown, trainVehicleID, Vehicles.Trains);
                fillboxes(trailDropDown, trailVehicleID, Vehicles.Trailers);
                fillboxes(convDropDown, convVehicleID, Vehicles.Convertables);
                btnSave.Enabled = btnSaveAs.Enabled = true;
            }
        }
        void fillboxes(ComboBox cb, DomainUpDown dud, Data[] data)
        {
            cb.Items.Clear();
            dud.Items.Clear();
            foreach (Data info in data)
            {
                cb.Items.Add(info.Name);
                dud.Items.Add(info.Name);
            }
            if (dud.Items.Count >= 1) dud.SelectedIndex = 0;
        }

        private void Cardatachanged(object sender, EventArgs e)
        {
            if (!updating)
            {
                Vehicles.Cars[CarVehicleID.SelectedIndex] = updatecardata(Vehicles.Cars[CarVehicleID.SelectedIndex]);
            }
        }

        private void txtleave(object sender, EventArgs e)
        {
            if (!updating)
            {
                TextBox data = (TextBox)sender;
                if (data.Text.Replace(" ", "").Replace("\t", "") == "")
                {
                    MessageBox.Show("Error, Data field cant be empty");
                    scriptfocus = true;
                    data.Text = tempdata;
                    data.Focus();
                }
            }
        }
        
        private void txtenter(object sender, EventArgs e)
        {
            TextBox data = (TextBox)sender;
            if (!scriptfocus) tempdata = data.Text;
            scriptfocus = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Utils.save(Filename, FileData, Vehicles);
        }

        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog { Filter = "Handling files|handling*.dat|data files|*.dat|All Files|*.*", FileName = new FileInfo(Filename).Name };
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Filename = sfd.FileName;
                Utils.save(Filename, FileData, Vehicles);
            }
        }

        private void CarCbChanged(object sender, EventArgs e)
        {
            CarVehicleID.SelectedIndex = CarDropDown.SelectedIndex;
        }

        private void boatCbChanged(object sender, EventArgs e)
        {
            boatVehicleID.SelectedIndex = boatDropDown.SelectedIndex;
        }

        private void bikeCbChanged(object sender, EventArgs e)
        {
            bikeVehicleID.SelectedIndex = bikeDropDown.SelectedIndex;
        }

        private void planeCbChanged(object sender, EventArgs e)
        {
            planeVehicleID.SelectedIndex = planeDropDown.SelectedIndex;
        }

        private void weapCbChanged(object sender, EventArgs e)
        {
            weapVehicleID.SelectedIndex = weapDropDown.SelectedIndex;
        }

        private void subCbChanged(object sender, EventArgs e)
        {
            subVehicleID.SelectedIndex = subDropDown.SelectedIndex;
        }

        private void trainCbChanged(object sender, EventArgs e)
        {
            trainVehicleID.SelectedIndex = trainDropDown.SelectedIndex;
        }

        private void trailCbChanged(object sender, EventArgs e)
        {
            trailVehicleID.SelectedIndex = trailDropDown.SelectedIndex;
        }

        private void convCbChanged(object sender, EventArgs e)
        {
            convVehicleID.SelectedIndex = convDropDown.SelectedIndex;
        }

        private void CarDudChanged(object sender, EventArgs e)
        {
            CarDropDown.SelectedIndex = CarVehicleID.SelectedIndex;
            updatecartxtboxes(Vehicles.Cars[CarVehicleID.SelectedIndex]);
        }

        private void BoatDudChanged(object sender, EventArgs e)
        {
            boatDropDown.SelectedIndex = boatVehicleID.SelectedIndex;
        }

        private void bikeDudChanged(object sender, EventArgs e)
        {
            bikeDropDown.SelectedIndex = bikeVehicleID.SelectedIndex;
        }
        
        private void planeDudChanged(object sender, EventArgs e)
        {
           planeDropDown.SelectedIndex = planeVehicleID.SelectedIndex;
        }
        
        private void weapDudChanged(object sender, EventArgs e)
        {
            weapDropDown.SelectedIndex = weapVehicleID.SelectedIndex;
        }

        private void subDudChanged(object sender, EventArgs e)
        {
            subDropDown.SelectedIndex = subVehicleID.SelectedIndex;
        }

        private void trainDudChanged(object sender, EventArgs e)
        {
            trainDropDown.SelectedIndex = trainVehicleID.SelectedIndex;
        }

        private void trailDudChanged(object sender, EventArgs e)
        {
            trailDropDown.SelectedIndex = trailVehicleID.SelectedIndex;
        }

        private void convDudChanged(object sender, EventArgs e)
        {
            convDropDown.SelectedIndex = convVehicleID.SelectedIndex;
        }

        }
}
